package algorithm.niuke;
/*
5 10 25 26 40
5 15 0 15

25 10 40 5 26
25 10 40 5 26
m 左 右 左 右
大 小 小 大 大 小 小
1 2 3 4 5 6 7 8 9
2 9 1 
左 右 左 右 左 右 左 右左右左右左右 
*/
import java.util.*;
import java.math.*;

public class 疯狂队列 {
    public static void main(String[] arg) {
        Scanner cin = new Scanner(System.in);
        int total = cin.nextInt();
        List<Integer> heights = new ArrayList<>();
        for (int i = 0; i < total; i++) {
            heights.add(cin.nextInt());
        }
        Collections.sort(heights);
        String crazy = String.valueOf(heights.get(total - 1));
        boolean left = true;
        int smaller = 0;// 0 1 小 2 3 大
        int smallIndex = 0;
        int bigIndex = total - 2;
        for (int i = 0; i < total - 2; i++) {
            int num = -1;
            if (smaller <= 1) {
                num = heights.get(smallIndex++);
            } else {
                num = heights.get(bigIndex--);
            }
            if (left) {
                crazy = crazy + " " + num;
            } else {
                crazy = num + " " + crazy;
            }
            left = !left;
            smaller++;
            smaller = smaller % 4;
        }
        System.out.print(Math.max(calculate(crazy + " " + heights.get(smallIndex), total),
                calculate(heights.get(smallIndex) + " " + crazy, total)));
    }

    public static int calculate(String crazy, int total) {
        String[] numStrs = crazy.split(" ");
        int result = 0;
        for (int i = 1; i < total; i++) {
            int a = Integer.valueOf(numStrs[i - 1]);
            int b = Integer.valueOf(numStrs[i]);
            result += (Math.abs(a - b));
        }
        return result;
    }
}